package com.xjc.infra.code;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;

public class CodeCopyHes {

	public static String sourceFilePath = "D:\\tmp\\code\\";
//	public static String sourceFilePath = "F:\\SVN\\现网维护分支合并\\20170314-债转列表排序\\code\\";

	public static String source = "D:/projects/上上签/";
	//public static String source = "C:\\IFSProject\\niubility_v3\\";

	public static HashMap<String, String> folderRelation = new HashMap<String, String>();
	public static HashMap<JarLocation, Boolean> jarUpdated = new HashMap<JarLocation, Boolean>();

	public static String target = "D:\\tmp\\server\\";
//	public static String target = "F:\\SVN\\现网维护分支合并\\20170314-债转列表排序\\server\\";

//	public static String target = "F:\\SVN\\上线版本\\201703\\v17.11\\server\\";


//	public static String target = "F:\\SVN\\上线版本\\201702\\v_back_Hot-Deployment\\server\\";
//	public static String target = "F:\\SVN\\上线版本\\201704\\v17.16\\server\\";
	

	public static boolean isCmp = true;
	static {
		folderRelation.put("IFSCore", "core\\IFSXface");
		folderRelation.put("IFSXface", "core\\IFSXface");
		folderRelation.put("IFSInfoDisclosure", "infor\\IFSInfoDisclosure");
		folderRelation.put("IFSFront", "front\\ROOT,omp\\ROOT,sslpay\\ROOT");
		folderRelation.put("IFSH5api", "h5api\\ROOT");
		folderRelation.put("Org", "org\\ROOT");
		folderRelation.put("IFS", "p2p\\IFSFrameServer,pms\\IFSFrameServer,task\\IFSFrameServer");
		folderRelation.put("IFSFrameServer", "p2p\\IFSFrameServer,pms\\IFSFrameServer,task\\IFSFrameServer");
		folderRelation.put("IFSEntity-Base", "core\\IFSXface");
		folderRelation.put("IFS-Spring", "portal\\ROOT");
		folderRelation.put("IFSFrame", "p2p\\IFSFrameServer,pms\\IFSFrameServer,task\\IFSFrameServer");
		folderRelation.put("Crm", "crmmobile\\Crm");
		folderRelation.put("CrmServer", "crmserver\\CrmServer");
		folderRelation.put("IFSInteractive", "p2p\\IFSFrameServer,org\\ROOT,pms\\IFSFrameServer,task\\IFSFrameServer");
		folderRelation.put("IFSP2P", "p2p\\IFSFrameServer,pms\\IFSFrameServer,task\\IFSFrameServer");
		folderRelation.put("IFSParty", "p2p\\IFSFrameServer,pms\\IFSFrameServer,task\\IFSFrameServer");
		folderRelation.put("IFSWorkflow", "p2p\\IFSFrameServer,pms\\IFSFrameServer,task\\IFSFrameServer");
		folderRelation.put("IFSPortal", "portal\\ROOT");
		folderRelation.put("Payment", "payserver\\PaymentServer");
		folderRelation.put("Tranplatform", "Tranplatform\\Tranplatform");
		folderRelation.put("fund", "fund\\fund");
	}
	
	private static void codeCopy() throws Exception {
		File root = new File(sourceFilePath);
		showAllFiles(root);
		File sourcePath = null;
		File targetPath = null;
		for(JarLocation jar : jarUpdated.keySet()){
			sourcePath = new File(source + jar.getCompilePath() + "\\target\\" + jar.getJarName());
			System.out.println(sourcePath.getAbsolutePath());
			for(String targetName : jar.getTargetPath().split(",")){
				targetPath = new File(target + targetName + "\\WEB-INF\\lib\\"+ jar.getJarName());
				System.out.println(targetPath.getAbsolutePath());
				copyFolder(sourcePath, targetPath);
				
				File vsourcePath = new File(target + "\\version.txt");
				File vtargetPath = new File(target + targetName+ "\\version.txt");
				copyFolder(vsourcePath, vtargetPath);
			}
		}
	}

	final static void showAllFiles(File dir) throws Exception {
		File[] fs = dir.listFiles();

		for (int i = 0; i < fs.length; i++) {
			if (fs[i].isFile()) {
				
				String[] targetTitle = null;
				String tempSourcePath = null;
				String tempTargetPath = null;
				File sourcePath = null;
				File targetPath = null;
				String path = fs[i].getAbsolutePath();
				for (String key : folderRelation.keySet()) {
					if (path.indexOf(key + "\\src") != -1)
						targetTitle = folderRelation.get(key).split(",");
				}
			
				
				if(targetTitle == null){
					continue;
				}
				if (path.indexOf("webapp") != -1) {
					sourcePath = new File(path);
					for(int j = 0; j < targetTitle.length; j++){
						targetPath = new File(target + targetTitle[j] + path.split("webapp")[1]);
						copyFolder(sourcePath, targetPath);
					}
				}
				if(path.indexOf("resources") != -1 && path.indexOf("IFS\\src") == -1 && path.indexOf("IFS-Spring\\src")==-1){
					sourcePath = new File(path);
					for(int j = 0; j < targetTitle.length; j++){
						targetPath = new File(target + targetTitle[j] + "\\WEB-INF\\classes" + path.split("resources")[1]);
						copyFolder(sourcePath, targetPath);
//						copyFolder(targetPath, sourcePath);
					}
				}
				if(path.indexOf("main\\java") != -1){
					if(path.indexOf("IFSEntity-Base\\src") == -1 && path.indexOf("IFS\\src") == -1 && path.indexOf("IFS-Spring\\src")==-1){
						tempSourcePath = source + path.substring(sourceFilePath.length()).replace("src\\main\\java", "target\\classes");
						
						if(tempSourcePath.endsWith(".java"))
							tempSourcePath = tempSourcePath.replace(".java", ".class");
						tempTargetPath = tempSourcePath.split("classes")[1];
						sourcePath = new File(tempSourcePath);
						for(int j = 0; j < targetTitle.length; j++){
							targetPath = new File(target + targetTitle[j] + "\\WEB-INF\\classes" + tempTargetPath);
							copyFolder(sourcePath, targetPath);
						}
					}

					if(path.indexOf("\\entity\\") != -1 || path.indexOf("\\spinfra\\") != -1 || path.indexOf("\\interfaces\\") != -1 || path.indexOf("IFS\\src") != -1
							|| path.indexOf("\\com\\ifs\\infra\\helper\\") != -1 || path.indexOf("\\com\\ifs\\infra\\enumeration\\") != -1 || path.indexOf("\\client\\p2p\\") != -1
							|| path.indexOf("\\IFSEntity-Base\\") != -1 ){
						for(JarLocation jar : JarLocation.values()){
							for(String source : jar.getSourcePath().split(",")){
								if(path.indexOf(source) != -1 ){
									jarUpdated.put(jar, true);
								}
							}
						}
					}
				}
				for(int j = 0; j < targetTitle.length; j++){
					File vsourcePath = new File(target + "\\version.txt");
					File vtargetPath = new File(target + targetTitle[j]+ "\\version.txt");
					copyFolder(vsourcePath, vtargetPath);
				}
			}

			else if (fs[i].isDirectory()) {
				try {
					showAllFiles(fs[i]);
				} catch (Exception e) {
					e.printStackTrace();
					return;
				}
			}
		}
	}

	public static void main(String[] args) {
		try {
			codeCopy();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}

	}

	/**
	 * ����һ��Ŀ¼������Ŀ¼���ļ�������һ��Ŀ¼
	 * 
	 * @param src
	 * @param dest
	 * @throws IOException
	 */
	private static void copyFolder(File src, File dest) throws IOException {
		if(!src.isFile()){
			System.err.println(src.getAbsolutePath());
			throw new FileNotFoundException();
		}
		System.out.println(src.getAbsolutePath());
		System.out.println(dest.getAbsolutePath());
		File destDir = dest.getParentFile();
		
		if (!destDir.exists()) {
			destDir.mkdirs();
		}

		InputStream in = new FileInputStream(src);
		OutputStream out = new FileOutputStream(dest);

		byte[] buffer = new byte[1024];

		int length;

		while ((length = in.read(buffer)) > 0) {
			out.write(buffer, 0, length);
		}
		in.close();
		out.close();

	}

}
